package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.teacher.adapter.PetRankRvAdapter;
import com.ycsj.goldmedalnewconcept.teacher.bean.ClassInfo;
import com.ycsj.goldmedalnewconcept.teacher.bean.PetRankInfo;
import com.ycsj.goldmedalnewconcept.teacher.utils.ClassPoputil;
import com.ycsj.goldmedalnewconcept.teacher.utils.ClassUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 宠物排名
 * Created by Administrator on 2019/4/3 0003.
 */

public class TeacherPetRankingActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;
    @BindView(R.id.lv_edit)
    ImageView lvEdit;
    //适配器
    private PetRankRvAdapter adapter;

    //数据源
    private List<PetRankInfo.DataBean> oldDatas;

    //空视图
    private View emptyView;

    //所有班级的列表
    private List<ClassInfo.ListBean> classInfos;

    //记录弹出展示班级列表PopUpWindow的选中位置的值,默认第一个被选中
    private int popSelectedPos = 0;

    //请求参数
    private String acc, role, apptoken;


    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_pet_ranking;
    }

    @Override
    protected void init() {

        initStateBar();

        //初始化标题
        initTitle();

        //初始化请求参数
        initParams();

        //请求班级数据
        httpClass();
    }

    /**
     * 初始化请求参数
     */
    private void initParams() {
        acc = AccountUtils.getAccount(this);
        role = AccountUtils.getRole(this);
        apptoken = AccountUtils.getToken(this);
    }


    /**
     * 初始化标题栏
     */
    private void initTitle() {
        tvTitle.setText("宠物排名");
        lvEdit.setVisibility(View.VISIBLE);
        lvEdit.setImageResource(R.drawable.common_class);
    }

    /**
     * 请求班级里列表数据
     */
    private void httpClass() {
        ClassUtil.httpClass(rlLoading, this, new ClassUtil.onSuccessListener() {
            @Override
            public void onSuccess(List<ClassInfo.ListBean> datas) {
                //回调班级数据
                classInfos = datas;

                //证明有班级
                lvEdit.setVisibility(View.VISIBLE);

                //设置数据
                initRv();

                //请求页面列表数据
                httpPageData(popSelectedPos, BqaManager.NORML);
            }

            @Override
            public void hasNoData() {
                //没有班级,不显示顶部的切换班级的按钮和
                lvEdit.setVisibility(View.INVISIBLE);
            }
        });
    }

    /**
     * 初始化列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view_white_bg, null);
        adapter = new PetRankRvAdapter(R.layout.rv_pet_rank, oldDatas);
    }


    /**
     * 请求页面数据
     *
     * @param popSelectedPos 选中的班级位置
     * @param method         请求的方式
     */
    /**
     * 请求页面数据的方法
     *
     * @param popSelectedPos
     */
    private void httpPageData(int popSelectedPos, final int method) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "9");
        map.put("acc", acc);
        map.put("role", role);
        map.put("apptoken", apptoken);
        map.put("classnum", classInfos.get(popSelectedPos).getNum() + "");
        map.put("platform", "8");

        HttpUtils.post(HttpAddress.PETRANK, map, this, new HttpCallBack<PetRankInfo>() {
            @Override
            public void onSuccess(PetRankInfo info) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                if (llContent != null && llContent.getVisibility() != View.VISIBLE) {
                    llContent.setVisibility(View.VISIBLE);
                }

                if (null != info && "200".equals(info.getState())) {

                    //返回成功,设置列表数据
                    List<PetRankInfo.DataBean> newDatas = info.getData();

                    if (method == BqaManager.NORML) {
                        oldDatas.clear();
                        oldDatas.addAll(newDatas);
                        BqaManager.setRv(emptyView, TeacherPetRankingActivity.this, adapter, rv);
                    } else {
                        //更新数据
                        BqaManager.updateRvBySmf(method, null, rv, oldDatas, newDatas, adapter);
                        adapter.setEnableLoadMore(false);
                    }
                } else {
                    ToastUtils.showShort("返回器返回异常,错误码是:" + info.getState());
                    LogUtils.iTag("test", "返回器返回异常,错误码是:" + info.getState());
                }
            }

            @Override
            public void onError(String e) {
                BqaManager.setErrorNet(rlLoading, null, method, adapter);
            }
        });
    }


    /**
     * 选择班级的弹框
     */
    private void showClassPop() {
        ClassPoputil.showClassPop(this, classInfos, popSelectedPos, llTitle, new ClassPoputil.onPopItemClickListener() {
            @Override
            public void onItemClick(int pos) {
                //记录当前点击条目的位置
                popSelectedPos = pos;

                //请求页面数据
                httpPageData(popSelectedPos, BqaManager.NORML);
            }
        });
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_back, R.id.lv_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.lv_edit:
                showClassPop();
                break;
        }
    }
}
